﻿/**
*
* @usage	弹出窗口管理类,
* 			
* @problem	:1、AS2文档为什么可以得到_popBox.infor_text的zhi，而AS3不能直接读到，只能依靠帧监听得到?
*			2、单例模式为什么在public方法中取不到mc?
* <code>
* 	!!! 必须要有一个导出名为PopBox的MovieClip
* 	PopUpManager.initialize(pop_mc,stage.stageWidth/2,stage.stageHeight/2);
* </code>
**/
package org.nickro.project.objectInit{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.EventDispatcher;
	
	import org.nickro.project.events.TimelineEvent;
	import org.nickro.project.utils.TimelineWatcher;
	
	public class PopUpManager extends EventDispatcher{
		private static var mc:MovieClip;
		private static var _popX:Number;
		private static var _popY:Number;
		//初始化
		/**
		 * 
		 * @param	board_mc	弹出窗口时，遮盖部分
		 * @param	pop_x		popBox.x
		 * @param	pop_y		popBox.y
		 */
		public static function initialize(board_mc:MovieClip,pop_x:Number,pop_y:Number):void
		{
			mc = board_mc;
			_popX = pop_x;
			_popY = pop_y;
		}
		
		public static function popError(msg:String):void
		{
			PopUnique();
			mc.gotoAndPlay("off");
			var _popBox:PopBox = new PopBox();
			mc.addChild(_popBox);
			_popBox.gotoAndPlay("error");
			_popBox.name = "popbox";
			_popBox.x = _popX;
			_popBox.y = _popY;
			_popBox.addEventListener(Event.ENTER_FRAME, onLoop);
			function onLoop(e:Event):void {
				if (_popBox.infor_text) {
					_popBox.removeEventListener(Event.ENTER_FRAME, onLoop);
					_popBox.infor_text.text = msg;
					_popBox.firm_btn.addEventListener(MouseEvent.MOUSE_DOWN, function() { PopUpManager.deletePop(); } );
				}
			}
		}
		
		public static function popSuccess(msg:String,submitHandler:Function = null):void
		{
			PopUnique();
			mc.gotoAndPlay("off");
			var _popBox:PopBox = new PopBox();
			mc.addChild(_popBox);
			_popBox.name = "popbox";
			_popBox.gotoAndPlay("success");
			_popBox.x = _popX;
			_popBox.y = _popY;
			_popBox.addEventListener(Event.ENTER_FRAME, onLoop);
			function onLoop(e:Event):void {
				if (_popBox.infor_text) {
					_popBox.removeEventListener(Event.ENTER_FRAME, onLoop);
					_popBox.infor_text.text = msg;
					_popBox.firm_btn.addEventListener(MouseEvent.MOUSE_DOWN, function() { PopUpManager.deletePop();
						if (submitHandler != null) submitHandler.apply(null, null);
					} );
				}
			}
		}
		
		public static function popSendwait(msg:String):void
		{
			PopUnique();
			mc.gotoAndPlay("off");
			var _popBox:PopBox = new PopBox();
			mc.addChild(_popBox);
			_popBox.name = "popbox";
			_popBox.gotoAndPlay("wait");
			_popBox.x = _popX;
			_popBox.y = _popY;
			_popBox.addEventListener(Event.ENTER_FRAME, onLoop);
			function onLoop(e:Event):void {
				if (_popBox.infor_text) {
					_popBox.removeEventListener(Event.ENTER_FRAME, onLoop);
					_popBox.infor_text.text = msg;
				}
			}
		}
		/**
		 * 附加的短视功能
		 */
		public static function lockScreen():void
		{
			mc.gotoAndPlay("off");
		}
		
		public static function unlockScreen():void
		{
			mc.gotoAndPlay("on");
		}
		
		public static function die():void
		{
			deletePop();
		}
		
		private static function deletePop():void
		{
			mc.gotoAndPlay("on");
			mc.removeChild(mc.getChildByName("popbox"));
		}
		//popbox唯一存在
		private static function PopUnique():void {
			if (mc.getChildByName("popbox")) {
				mc.removeChild(mc.getChildByName("popbox"));
			}
		}
	}
}
